<template>
  <div
    v-loading="loading"
    class="home-container"
    ref="container"
    @wheel="handleWheel"
  >
    <ul
      class="carousel-container"
      :style="{
        marginTop,
      }"
      @transitionend="handleTransTionEnd"
    >
      <li v-for="item in data" :key="item.id">
        <!-- 这个地方要注意下标i  这里循环的data 就是仓库里面的数据了 -->
        <CarouselItem :carousel="item" ref="child" />
      </li>
    </ul>
    <div @click="switchTo(index - 1)" v-show="index >= 1" class="icon icon-up">
      <Icon type="arrowUp" />
    </div>
    <div
      @click="switchTo(index + 1)"
      v-show="index < data.length - 1"
      class="icon icon-down"
    >
      <Icon type="arrowDown" />
    </div>
    <ul class="indicator">
      <li
        :class="{ active: i === index }"
        v-for="(item, i) in data"
        :key="item.id"
        @click="switchTo(i)"
      ></li>
    </ul>
    <!-- <Loading v-if="isLoading" /> -->
  </div>
</template>

<style lang="less" scoped>
@import "~@/styles/mixin.less";
@import "~@/styles/var.less";
.home-container {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;

  //测试
  // width: 60%;
  // height: 40%;
  // overflow: visible;
  // border: 2px solid #008c8c;
  // margin: 100px auto;

  ul {
    margin: 0;
  }

  .carousel-container {
    width: 100%;
    height: 100%;
    transition: 500ms;
    li {
      width: 100%;
      height: 100%;
    }
  }
  .icon {
    .self-center();
    font-size: 30px;
    @gap: 10px;
    color: @gray;
    cursor: pointer;
    transform: translateX(-50%);
    &.icon-up {
      top: @gap;
      animation: jump-up 2s infinite;
    }
    &.icon-down {
      top: auto;
      bottom: @gap;
      animation: jump-down 2s infinite;
    }
    @jump: 5px;
    @keyframes jump-up {
      0% {
        transform: translate(-50%, @jump);
      }
      50% {
        transform: translate(-50%, -@jump);
      }
      100% {
        transform: translate(-50%, @jump);
      }
    }
    @keyframes jump-down {
      0% {
        transform: translate(-50%, -@jump);
      }
      50% {
        transform: translate(-50%, @jump);
      }
      100% {
        transform: translate(-50%, -@jump);
      }
    }
  }
  .indicator {
    position: absolute;
    top: 50%;
    right: 10px;
    transform: translateY(-50%);
    margin: 0;
    li {
      width: 10px;
      height: 10px;
      border: 1px solid #fff;
      margin: 5px 0;
      border-radius: 50px;
      background: @words;
      box-sizing: border-box;
      transition: 0.4s;
      cursor: pointer;
      &.active {
        background: #fff;
      }
    }
  }
}
</style>

<script>
//导入vuex
import { mapState } from "vuex";
//import { getBanners } from "@/api/banner"; //不需要了 直接从仓库拿
import CarouselItem from "./Carouselitem.vue";
import Icon from "@/components/Icon";
// import Loading from "@/components/Loading";
// import fetchData from "@/mixins/fetchData.js"; //也是从仓库拿

export default {
  //mixins: [fetchData([])], //我们已经从仓库里面拿数据了 所以就不用在调用ajax的请求了
  components: {
    CarouselItem,
    Icon,
    //Loading,
  },
  data() {
    return {
      index: 0, //来储存当前是第几张图片
      containerHeight: 0, //储存当前窗口的高度
      switching: false, //用来存储当前的滚轮是否在翻动
    };
  },
  created() {
    //从仓库拿数据
    this.$store.dispatch("banner/fetchBanner");
  },
  mounted() {
    //用来计算当前页面的可使高度
    this.containerHeight = this.$refs.container.clientHeight;
    window.addEventListener("resize", this.handleResize);
  },
  destroyed() {
    window.removeEventListener("resize", this.handleResize);
  },
  computed: {
    marginTop() {
      //用来计算当前是第几张图 需要向上移多少高度
      return -this.index * this.containerHeight + "px";
    },
    //把拿过来的数据进行展开
    ...mapState("banner", ["loading", "data"]),
  },
  methods: {
    async fetchData() {
      return await getBanners();
    },
    switchTo(i) {
      this.index = i;
    },
    handleWheel(e) {
      if (this.switching) {
        return;
      }
      if (e.deltaY < -2 && this.index > 0) {
        this.switching = true;
        this.index--;
        this.$nextTick(() => {
          const childComponent = this.$children.find(
            (components) => components.$options.name === "CarouselItem"
          );
          if (childComponent) {
            console.log("childComponent 里面有值");
            childComponent.showWords();
          }
        });
      } else if (e.deltaY > 2 && this.index < this.data.length - 1) {
        this.switching = true;
        this.index++;
        this.$nextTick(() => {
          const childComponent = this.$children.find(
            (component) => component.$options.name === "CarouselItem"
          );
          if (childComponent) {
            childComponent.showWords();
          } else {
            console.log(childComponent);

            console.log("没有运行if里面的内容");
          }
        });
      }
    },
    handleTransTionEnd() {
      this.switching = false;
    },
    handleResize() {
      this.containerHeight = this.$refs.container.clientHeight;
      console.log("试图窗口改变");
    },
  },
};
</script>
